#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
IR Delete 语句节点定义
"""

import ast
from dataclasses import dataclass, field
from typing import Iterator, List
from .base import IRStmt
from ..base import IRNode
from ..expressions import build_expr_from_ast


@dataclass
class IRDelete(IRStmt):
    """Delete 语句节点（del statement）"""

    targets: List[IRNode] = field(default_factory=list)  # 要删除的目标列表
    node_type: str = "delete"

    def get_child_nodes(self) -> Iterator[IRNode]:
        """获取所有子节点"""
        for target in self.targets:
            yield target
        yield from super().get_child_nodes()

    def build_from_ast(self, node: ast.Delete) -> None:
        """从 AST 节点构建 Delete 节点

        Args:
            node: ast.Delete 节点
        """
        self.targets = []
        for target in node.targets:
            target_expr = build_expr_from_ast(target, self)
            self.targets.append(target_expr)
